package cn.lecosa.spark.es.java;

import cn.lecosa.es.EsClient;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class CompoundQueryTest {
	private static TransportClient client = EsClient.eSClient();
	
	public static void main(String[] args) throws UnknownHostException {
		//constantScoreQuery(client);
		booQuery();
//		disMaxQuery(client);
//		client.close();
	}
	
	public static void constantScoreQuery(Client client ) {
		SearchResponse res = null;
		QueryBuilder qb = QueryBuilders
				.constantScoreQuery(QueryBuilders.termQuery("title","article"))
				.boost(2.0f);
		
		res = client.prepareSearch("search_test")
				.setTypes("article")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				.setQuery(qb)
				.setFrom(0)
				.setSize(10)
				.execute().actionGet();
		for (SearchHit hit: res.getHits().getHits()) {
			System.out.println(hit.getSourceAsString());
		}
		
		// on shutdown
		client.close();
	}
	
	public static void booQuery( ) {
		Long[] gids = {110000185L,110000181L};
		SearchResponse res = null;
//		BoolQueryBuilder q = QueryBuilders.boolQuery().filter(QueryBuilders.termsQuery("gid", gids));
//		QueryBuilder qb = QueryBuilders.boolQuery()
//				.must(QueryBuilders.termQuery("gid",gids ))
//				.mustNot(QueryBuilders.termQuery("title","relevance"))
//				.should(QueryBuilders.termQuery("title","article"))
//				.filter(QueryBuilders.termQuery("imsi",new String[] {"460007621744514","460007621742601"}))
				;
				BoolQueryBuilder q = QueryBuilders.boolQuery().filter(QueryBuilders.termsQuery("gid", gids));
		res = client.prepareSearch("visual")
				.setTypes("imsi-data")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				.setQuery(q)
				.setFrom(0)
				.setSize(10)
				.execute().actionGet();
		for (SearchHit hit: res.getHits().getHits()){
			System.out.println(hit.getSourceAsString());
		}
		
		// on shutdown
		client.close();
	}
	
	public static void disMaxQuery(Client client ) {
		SearchResponse res = null;
		QueryBuilder qb = QueryBuilders
				.disMaxQuery()
				.add(QueryBuilders.termQuery("title","article"))
				.add(QueryBuilders.termQuery("title","relevance")).boost(1.2f).tieBreaker(0.7f);
		
		res = client.prepareSearch("search_test")
				.setTypes("article")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				.setQuery(qb)
				.setFrom(0)
				.setSize(10)
				.execute().actionGet();
		for (SearchHit hit: res.getHits().getHits()) { 
			System.out.println(hit.getSourceAsString());
		}
		
		// on shutdown
		client.close();
	}
	
}
